package de.home.develop.firstgame.logic
{
		
	import flash.events.*;
	import flash.geom.*;	
	import mx.core.*;
	
	public class Ball extends AnimatedGameObject
	{
		
		
		protected var speedY:Number = 0.0;
		protected var speedX:Number = 0.0;
		
		protected static const GRAVITIY:Number = 0.1;
		protected static const SURFACE:Number = 480;
		protected static const FRICTION:Number = 0.996;
		
		protected static const START_BALL_X:Number = 500;
		protected static const START_BALL_Y:Number = 300;
		
		public function Ball()
		{
		}
		
		override public function shutdown():void		
		{			
			super.shutdown();			
		}
		
		
		public function startupBall():void		
		{			
			super.startupAnimatedGameObject(ResourceManager.BallGraphics, new Point(FlexGlobals.topLevelApplication.width / 2, 300), ZOrders.BallZOrder);					
			this.collisionName = CollisionIdentifiers.BALL;
		}
		
		
		private function resetBall():void
		{
			position.y = START_BALL_Y;
			position.x = START_BALL_X;
			speedX = 0;
			speedY = 0;
		}
		
		
		
		private function moveBall(dt:Number):void
		{			
			if(SURFACE < position.y)
			{
				speedY = -speedY;
				position.y = SURFACE + GRAVITIY;
				
			}
			position.y -= speedY;
			speedY -= GRAVITIY;
			speedY *= FRICTION;
			
			
			if(Math.abs(speedX) > 0.1)
			{
				speedX *= FRICTION;
				position.x -= speedX;
			}
			
			if(position.x <= 5 || position.x >= (FlexGlobals.topLevelApplication.width - (frameWidth + 5)))
			{				
				speedX *= -1;
				if(position.x <= 5)
				{
					position.x = 5;
				}
				
				if(position.x  >= (FlexGlobals.topLevelApplication.width - (frameWidth + 5)))
				{
					position.x = FlexGlobals.topLevelApplication.width - (frameWidth + 5);
				}
			}
			
			
			
			if(position.y <= 5 || position.y >= FlexGlobals.topLevelApplication.height)
			{
				speedY *= -1;
				
				if(position.y <= 5)
				{
					position.y = 5;
				}
				
				if(position.y  >= (FlexGlobals.topLevelApplication.height - (frameWidth + 5)))
				{
					position.y = FlexGlobals.topLevelApplication.height - (frameWidth + 5);
				}
				
			}
			
			if(position.y >= SURFACE)
			{
				resetBall();
			}
			
			
		}
		
		override public function enterFrame(dt:Number):void		
		{			
			super.enterFrame(dt);
			if(speedX != 0 || speedY != 0)
			{
				moveBall(dt);	
			}				
			
			
		}
		
		override public function collision(other:GameObject):void
		{						
			
			var dX:Number = position.x - other.position.x;
			var dY:Number = position.y - other.position.y;
			var TreffWinkel:Number = Math.atan2(dY, dX);
	//		trace(dX,dY,TreffWinkel);
		   
		//    vb=Math.sqrt(B.Speed_X*B.Speed_X+B.Speed_Y*B.Speed_Y);
		    var ma:Number = this.CollisionArea.height;  
		    var mb:Number = other.CollisionArea.height;
		   var  f:Number = ma/mb;
		  //    c=(ma*va+mb*vb)/(ma+mb);
		//   trace("ma/mb:" + f);
		     this.speedX = 6 / f * Math.cos(TreffWinkel+Math.PI);
		     this.speedY = 8 / f * Math.sin(TreffWinkel+Math.PI);
			//trace("xFak: " + (f * Math.cos(TreffWinkel+Math.PI)) + " yFak: " + (f * Math.sin(TreffWinkel+Math.PI))); 
			//           B.Speed_X = c*f*Math.cos(TreffWinkel);
			//           B.Speed_Y = c*f*Math.sin(TreffWinkel);
			//trace("speed: "+ speedX,speedY);
			
			
		}
			
		
		
		
	}
}